import { getBlackCompanyIds } from '../service/post-controller'
import { showToast } from '../crossplatform/apiservice/toast'
import { getStore } from '../store/index'
import { UPDATEIM } from '../store/types/im'
import { UPDATEUSERINFO } from '../store/types/userinfo'
import { switchTab } from '../crossplatform/apiservice/navigate'
import pagejumplist from '../configuration/pagejumplist.json'

// store
const store = getStore()
// 判断当前b端用户是否被拉黑
const judgeBlack = {
  getBlackCompanyIds: (companyId, isJump = false) => {
    return new Promise(resolve => {
      getBlackCompanyIds().then(({ data }) => {
        const { entry, status } = data
        if (status && companyId) {
          if (judgeBlack.isInBlackList(companyId, entry)) {
            // 该公司已经被拉黑
            showToast({
              title: '贵公司已被拉黑，无法进行任何操作'
            })
            if(!isJump) {
              judgeBlack.updateUserState({
                role: 'personal'
              })
              judgeBlack.updateImState({
                contactList: []
              })
              let timer = setTimeout(() => {
                clearTimeout(timer)
                switchTab({
                  url: pagejumplist['self-center'].path
                })
              }, 500)
            }
            resolve(true)
          } else {
            resolve(false)
          }
        }
      })
    })
  },
  isInBlackList: (id, blackList) => {
    for (let i in blackList) {
      if (id == blackList[i]) {
        return true
      }
    }
    return false
  },
  updateImState(payload) {
    store.dispatch({
      type: UPDATEIM,
      payload: {
        ...payload
      }
    })
  },
  updateUserState(payload) {
    store.dispatch({
      type: UPDATEUSERINFO,
      payload: {
        ...payload
      }
    })
  },
}
export default judgeBlack
